//
//  HouseSelectVC.m
//  SmartEstateCC
//
//  Created by Steven on 2019/6/22.
//  Copyright © 2019 pretang. All rights reserved.
//

#import "HouseSelectVC.h"
#import "HouseCell.h"
#import "InputVC.h"

@interface HouseSelectVC ()<UITableViewDelegate, UITableViewDataSource>

@property(weak, nonatomic)IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;
@property (weak, nonatomic) IBOutlet UIButton *inputBtn;

//三处data
@property(nonatomic, strong)NSArray *houseDatas; //楼栋
@property(nonatomic, strong)NSArray *cellDatas;//单元
@property(nonatomic, strong)NSArray *roomDatas;//房间

@property(nonatomic, strong)NSString *houseText;
@property(nonatomic, strong)NSString *cellText;
@property(nonatomic, strong)NSString *roomText;


@property(nonatomic, strong)NSArray *tempArray;


@end

@implementation HouseSelectVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES];
    [self getData];
    
    self.tableView.tableFooterView = [UIView new];
    
    self.param = [@{@"nameId": @"64", @"buildingId": @"1865"} mutableCopy];
    
}

- (void)getData{
    
    //楼栋号
    __weak typeof(self) weakSelf = self;
//    [CTNetwork.sharedInstance POST:@"app/secondHandHouse/searchHousingBuildingNoForHmf" parameters: @{@"nameId": @"64", @"buildingId": @"1865"} completion:^(id response, NSError *error) {
//
//        weakSelf.houseDatas = response[@"data"];
//
//        weakSelf.tableView.reloadData;
//        NSLog(@"-------------%@", response)
//
//    }];
    
   
}


//获取单元数据
- (void)getCellDatas: (NSMutableDictionary *)houseData{
    __weak typeof(self) weakSelf = self;
//    [CTNetwork.sharedInstance POST:@"app/secondHandHouse/searchHousingUnitForHmf" parameters: houseData completion:^(id response, NSError *error) {
//        weakSelf.cellDatas = response[@"data"];
//        [weakSelf.cancelBtn setTitle:@"上一步" forState: UIControlStateNormal];
//        [weakSelf.tableView reloadData];
//        NSLog(@"-------------%@", response)
//    }];
}

//获取房门号
- (void)getRoomDatas: (NSMutableDictionary *)houseData{
    __weak typeof(self) weakSelf = self;
//    [CTNetwork.sharedInstance POST:@"app/secondHandHouse/searchHousingDoorNoForHmf" parameters: houseData completion:^(id response, NSError *error) {
//        weakSelf.roomDatas = response[@"data"];
//        [weakSelf.cancelBtn setTitle:@"上一步" forState: UIControlStateNormal];
//        [weakSelf.tableView reloadData];
//        NSLog(@"-------------%@", response[@"data"])
//    }];
}




//取消按钮
- (IBAction)cancelAction:(UIButton *)sender{
    
    if ([self.descLabel.text isEqual: @"楼栋号"]) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }else if ([self.descLabel.text isEqual: @"单元号"]){
        
        [self.param removeObjectForKey:@"buildingNo"];
        self.descLabel.text = @"楼栋号";
        
        [self.tableView reloadData];
        
    }else{
         [self.param removeObjectForKey:@"unit"];
         self.descLabel.text = @"单元号";
         [self.tableView reloadData];
    }
    
}


//手动输入按钮
- (IBAction)inputAction:(id)sender{
    
    InputVC *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"InputVC"];
    
    __weak typeof(self) weakSelf = self;
    vc.onCompleted = ^(NSString *houseNo, NSString *cellNo, NSString *roomNo) {
        
        [weakSelf.navigationController dismissViewControllerAnimated:NO completion:^{
            !weakSelf.onCompleted ?: weakSelf.onCompleted(houseNo, cellNo, roomNo);
        }];
    };
    [self.navigationController pushViewController:vc animated:YES];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([self.descLabel.text isEqual: @"楼栋号"]) {
        return _houseDatas.count;
    }else if ([self.descLabel.text isEqual: @"单元号"]){
        return _cellDatas.count;
    }else{
        return _roomDatas.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HouseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HouseCell" forIndexPath:indexPath];
    if ([self.descLabel.text isEqual: @"楼栋号"]) {
        NSDictionary* data = self.houseDatas[indexPath.row];
        cell.titleLabel.text = data[@"buildingNo"];
        
    }else if ([self.descLabel.text isEqual: @"单元号"]){
        NSDictionary* data = self.cellDatas[indexPath.row];
        cell.titleLabel.text = [NSString stringWithFormat:@"%@单元",data[@"unit"]];
        
    }else {
        NSDictionary* data = self.roomDatas[indexPath.row];
        cell.titleLabel.text = data[@"doorNo"];
    }
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.descLabel.text isEqual: @"楼栋号"]) {
        NSDictionary *data = self.houseDatas[indexPath.row];

        [self.param setObject:data[@"buildingNo"] forKey:@"buildingNo"];
        
        self.houseText = data[@"buildingNo"];
        self.descLabel.text = @"单元号";
        //跳转到下一步请求
        [self getCellDatas: self.param];
        
    }else if ([self.descLabel.text isEqual: @"单元号"]){
        
        NSDictionary *data = self.cellDatas[indexPath.row];
        self.cellText = data[@"unit"];
        self.descLabel.text = @"门牌号";
        [self.param setObject:data[@"unit"] forKey:@"unit"];
        //跳转到下一步请求
        [self getRoomDatas:self.param];
        
    }else{
        NSDictionary *data = self.roomDatas[indexPath.row];
        self.roomText = data[@"doorNo"];
        
        [self dismissViewControllerAnimated:true completion:^{
            !self.onCompleted ?: self.onCompleted(self.houseText, self.cellText, self.roomText);
        }];
    }
}


@end

